package com.shengyijie.activity;
  
import org.apache.http.HttpResponse;

import com.shengyijie.activity.tab.AboutActivity;
import com.shengyijie.context.Commen;
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.database.dataoperate.AccountInfoDB;  
import com.shengyijie.model.http.HttpConnectApi;
import com.shengyijie.model.json.JsonParser;
import com.shengyijie.util.DataStore;

import android.app.Activity; 
import android.content.Context;
import android.content.Intent; 
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout; 
import android.widget.RelativeLayout;

public class StartActivity extends Activity{
  
	 LinearLayout ddd;
	 public boolean isGuide=true;
	 protected Animation animation1;
	 protected Animation animation2;
	 protected Animation animation3;
	 protected Animation animation4;
	 protected Animation animation6;
	 protected Animation animation7; 
	 protected Animation animation0;
	 protected Animation animation;
	 
	 RelativeLayout wel;
	 ImageView logo1;
	 ImageView logo2;
	 ImageView logo3;
	 ImageView logo4;
	 ImageView logo5;
	 ImageView logo6;
	 ImageView logo7;
	 ImageView logo;
	 
	 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.start);   
        ddd=(LinearLayout) this.findViewById(R.id.aaa); 
        getAbout();
        initView();
        animation1();
        initUserTable();
        Commen.userKey(StartActivity.this);
        getNewsType();
     } 
	 
	 public void initView(){
		logo=(ImageView) this.findViewById(R.id.logo);
        logo1=(ImageView) this.findViewById(R.id.logo1);
        logo2=(ImageView) this.findViewById(R.id.logo2);
        logo3=(ImageView) this.findViewById(R.id.logo3);
        logo4=(ImageView) this.findViewById(R.id.logo4);
        logo5=(ImageView) this.findViewById(R.id.logo5);
        logo6=(ImageView) this.findViewById(R.id.logo6);
        logo7=(ImageView) this.findViewById(R.id.logo7);
        
        wel=(RelativeLayout) this.findViewById(R.id.wel);
	 }
	 
	 public void animation1(){
		    animation1 = AnimationUtils.loadAnimation(this, R.anim.translate_1);
	        logo1.startAnimation(animation1);
	        
	        animation2 = AnimationUtils.loadAnimation(this, R.anim.translate_2);
	        logo2.startAnimation(animation2);
	        
	        animation3 = AnimationUtils.loadAnimation(this, R.anim.translate_3);
	        logo3.startAnimation(animation3);
	        
	        animation4 = AnimationUtils.loadAnimation(this, R.anim.translate_4);
	        logo4.startAnimation(animation4);
	        
	        animation6 = AnimationUtils.loadAnimation(this, R.anim.translate_6);
	        logo6.startAnimation(animation6);
	        
	        animation7 = AnimationUtils.loadAnimation(this, R.anim.translate_7);
	        logo7.startAnimation(animation7);
	         
	        animation7.setAnimationListener(animationListener1); 
	 }
	 
	 AnimationListener animationListener1= new AnimationListener() {  
     	@Override  
     	public void onAnimationStart(Animation animation) {  
     	}  
     	  
     	@Override  
     	public void onAnimationRepeat(Animation animation) {  
     	}  
     	  
     	@Override  
     	public void onAnimationEnd(Animation animation) {  
     		logo1.setVisibility(View.INVISIBLE);
     		logo2.setVisibility(View.INVISIBLE);
     		logo3.setVisibility(View.INVISIBLE);
     		logo4.setVisibility(View.INVISIBLE);
     		logo5.setVisibility(View.INVISIBLE);
     		logo6.setVisibility(View.INVISIBLE);
     		logo7.setVisibility(View.INVISIBLE); 
     		logo.setVisibility(View.VISIBLE);
     		animation2();
     	}  
     };
     
     
     public void animation2(){
    	 animation0 = AnimationUtils.loadAnimation(StartActivity.this, R.anim.translate_0);
         logo.startAnimation(animation0); 
         animation0.setAnimationListener(animationListener2); 
	 }
	 
	 AnimationListener animationListener2= new AnimationListener() {  
	  	@Override  
	  	public void onAnimationStart(Animation animation) {  
	  	}  
	  	  
	  	@Override  
	  	public void onAnimationRepeat(Animation animation) {  
	  	}  
	  	  
	  	@Override  
	  	public void onAnimationEnd(Animation animation) {   
	  		logo.setVisibility(View.INVISIBLE);
	  		animation = AnimationUtils.loadAnimation(StartActivity.this, R.anim.alpha_wel);
	  		wel.setAnimation(animation);
	  		wel.setBackgroundResource(R.drawable.welcome);
//	  		
//	  		wel.setAnimation(animation);
//	  		wel.startAnimation(animation);
	  		toMainActicity();
	  	}  
     };
	 
	 
	public void toMainActicity(){ 
			Handler x = new Handler();
	        x.postDelayed(new splashhandler(), 1000); 
	}
    class splashhandler implements Runnable{
        public void run() {
        	Intent intent; 
        	intent=new Intent(getApplication(),MainActivity.class);
            startActivity(intent);
            StartActivity.this.finish();
            if(Build.VERSION.SDK_INT >=5){
            	 overridePendingTransition(R.anim.zoomin, R.anim.zoomout); 
            }
        }
    }
    
    
    public void initUserTable(){
		 try{
			 AccountInfoDB dh=new AccountInfoDB(this, "shengyijie", 1);
			 dh.openWrite(); 
			 if(!dh.isTableExits(AccountInfoDB.TABLE_ACCOUNT)){
		        dh.createTable();	
			 }
			 Cursor cursor=dh.fetchAllRecords();
			 ContextApplication.user=ContextApplication.user.getUserFromCursor(cursor);
			 cursor.close();
			 dh.close();
			 if(ContextApplication.user!=null&&ContextApplication.user.getIsRemember()==1){
				 ContextApplication.isUserLogin=true;
			 }
	     }catch(Exception e){}
	}
	 
	
    public void getAbout(){
		 new Thread() {
	            @Override
	            public void run() {
	           	   try{
	           		         HttpConnectApi httpConnect=HttpConnectApi.getInstance(); 
                             HttpResponse response=httpConnect.getAbout(AboutActivity.Page_Help);
                             if (response.getStatusLine().getStatusCode() == 200)
            			     {
                       	        JsonParser jsonParser=JsonParser.getInstance(); 
                       	        String content=jsonParser.getAbout(response, AboutActivity.Page_Help); 
                       	        if(content!=null&&content!=""){
                       	        	DataStore.store(StartActivity.this, AboutActivity.Page_Help, content);
                       	        } 
        	                 }
                             
                             httpConnect=HttpConnectApi.getInstance(); 
                             response=httpConnect.getAbout(AboutActivity.Page_About);
                             if (response.getStatusLine().getStatusCode() == 200)
            			     {
                       	        JsonParser jsonParser=JsonParser.getInstance(); 
                       	        String content=jsonParser.getAbout(response, AboutActivity.Page_About); 
                       	        if(content!=null&&content!=""){
                       	        	DataStore.store(StartActivity.this, AboutActivity.Page_About, content);
                       	        } 
        	                 }
                             
                             httpConnect=HttpConnectApi.getInstance(); 
                             response=httpConnect.getAbout(AboutActivity.Page_Contact);
                             if (response.getStatusLine().getStatusCode() == 200)
            			     {
                       	        JsonParser jsonParser=JsonParser.getInstance(); 
                       	        String content=jsonParser.getAbout(response, AboutActivity.Page_Contact); 
                       	        if(content!=null&&content!=""){
                       	        	DataStore.store(StartActivity.this, AboutActivity.Page_Contact, content);
                       	        } 
        	                 }
	               }catch (Exception e) {}  
	            }
	        }.start();
	     }
	 
    
    public void getNewsType(){
		 new Thread() {
	            @Override
	            public void run() {
	           	      try{
	           		        HttpConnectApi httpConnect=HttpConnectApi.getInstance(); 
                            HttpResponse response=httpConnect.getNewsType("");
                            if (response.getStatusLine().getStatusCode() == 200)
           			        {
                      	        JsonParser jsonParser=JsonParser.getInstance(); 
                      	         ContextApplication.list=jsonParser.getNewsType(response); 
//                      	        if(content!=null&&content!=""){
//                      	        	DataStore.store(StartActivity.this, AboutActivity.Page_Help, content);
//                      	        } 
       	                 }
	                 }catch (Exception e) {}  
	            }
	        }.start();
	     }
}
